package BinaryTree;

/**
 * User:辰柒
 * Date:2022-09-26
 * Time:19:10
 */
public class TestBinaryTree {
      public class TreeNode {
         char val;
         TreeNode left;
         TreeNode right;
         TreeNode() {

         }
         TreeNode(char val) {

             this.val = val;
         }
         TreeNode(char val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
    }
 }

    public TreeNode createTree() {//创建二叉树
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }


    public void preOrder(TreeNode root){//前序遍历
         if(root == null){
             return;
         }
         System.out.print(root.val+" ");
         preOrder(root.left);
         preOrder(root.right);
    }

    public void inOrder(TreeNode root){//中序遍历
        if(root == null){
            return;
        }
        preOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }


    public void postOrder(TreeNode root){//后序遍历
        if(root == null){
            return;
        }
        preOrder(root.left);
        inOrder(root.right);
        System.out.print(root.val+" ");
    }


    /**
     * 获取树中节点的个数 两种方法
     */
    public static int nodeSize = 0;
    public int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }

    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int tmp = size2(root.left)+size2(root.right)+1;
        return tmp;
    }


    /**
     * 获取叶子节点的个数
     * @param root
     * @return
     */
    int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        int tmp = getLeafNodeCount(root.left) +
                getLeafNodeCount(root.right);
        return tmp;
    }

    public static int leafSize = 0;
    void getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return ;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }


    /**
     * 判断第K层节点的个数
     */
    int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null || k <= 0) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        int tmp = getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);
        return tmp;
    }


    /**
     *  获取二叉树的高度  时间复杂度：O(n)
     * @param root
     * @return
     */
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;
    }

    // 时间复杂度：O(n)
    public int getHeight2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return getHeight2(root.left) > getHeight2(root.right)
                ? getHeight2(root.left)+1 : getHeight2(root.right)+1;
    }


    /**
     * 检测值为value的元素是否存在
     * @param root
     * @param val
     * @return
     */
    public TreeNode find(TreeNode root, char val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode ret1 = find(root.left,val);
        if(ret1 != null) {
            return ret1;
        }
        TreeNode ret2 =  find(root.right,val);
        if(ret2 != null) {
            return ret2;
        }
        return null;
    }

    //时间复杂度：O(min(M,N))  P:M  Q:N
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q != null  || p != null && q == null ) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val) {
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }


    /**
     * 判断一棵树是否为另一颗树的子树
     */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null ) return false;
        //1、判断两棵树 是不是两颗相同的树
        if(isSameTree(root,subRoot)) return true;
        //2、subRoot是不是 root.left 子树
        if(isSubtree(root.left,subRoot)) return true;
        //3、subRoot是不是 root.right 子树
        if(isSubtree(root.right,subRoot)) return true;
        return false;
    }

    /**
     * 求树的最大深度
     */
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);
        return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;
    }

    /**
     * 对称二叉树
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }
    private boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree){
        if(leftTree == null && rightTree == null){
            return true;
        }
        if(leftTree==null || rightTree==null) {
            return false;
        }
        if(leftTree.val!=rightTree.val) {
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);
    }
}
